package com.ruoyi.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Viper;
import com.ruoyi.system.service.IViperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * viperController
 *
 * @author handsome
 * @date 2023-12-19
 */
@RestController
@RequestMapping("/system/viper")
public class ViperController extends BaseController {
    @Autowired
    private IViperService viperService;

    /**
     * 查询viper列表
     */
    @PreAuthorize("@ss.hasPermi('system:viper:list')")
    @GetMapping("/list")
    public TableDataInfo list(Viper viper) {
        startPage();
        List<Viper> list = viperService.selectViperList(viper);
        return getDataTable(list);
    }

    /**
     * 导出viper列表
     */
    @PreAuthorize("@ss.hasPermi('system:viper:export')")
    @Log(title = "viper", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Viper viper) {
        List<Viper> list = viperService.selectViperList(viper);
        ExcelUtil<Viper> util = new ExcelUtil<Viper>(Viper.class);
        util.exportExcel(response, list, "viper数据");
    }

    /**
     * 获取viper详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:viper:query')")
    @GetMapping(value = "/{vipID}")
    public AjaxResult getInfo(@PathVariable("vipID") Long vipID) {
        return success(viperService.selectViperByVipID(vipID));
    }

    /**
     * 新增viper
     */
    @PreAuthorize("@ss.hasPermi('system:viper:add')")
    @Log(title = "viper", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Viper viper) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = sdf.format(new Date());
        try {
            viper.setVipTime(sdf.parse(currentDate));
        } catch (ParseException e) {
            System.out.println(e);
        }
        return toAjax(viperService.insertViper(viper));
    }

    /**
     * 修改viper
     */
    @PreAuthorize("@ss.hasPermi('system:viper:edit')")
    @Log(title = "viper", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Viper viper) {
        return toAjax(viperService.updateViper(viper));
    }

    /**
     * 删除viper
     */
    @PreAuthorize("@ss.hasPermi('system:viper:remove')")
    @Log(title = "viper", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vipIDs}")
    public AjaxResult remove(@PathVariable Long[] vipIDs) {
        return toAjax(viperService.deleteViperByVipIDs(vipIDs));
    }
}
